﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace News.CYT369.Com.Service
{
    public class DocumentService: News.CYT369.Com.DAL.BaseService<DocumentService>
    {
        public DocumentService() { }

        #region DocumentType
        public News.CYT369.Com.Domains.UserDocCategory GetDocumentTypeByID(long typeId)
        {
            return base.GetOne<News.CYT369.Com.Domains.UserDocCategory>(c => c.Id == typeId);
        }

        #endregion

        #region Article
        /// <summary>
        /// 阅读次数
        /// </summary>
        /// <param name="docType"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateDocumentReadStatus(long id)
        {
            return base.DoAction<News.CYT369.Com.Domains.Document>(c => c.Id == id && c.Published == true && c.Deleted == false && c.AuditStatusID == 1,
               delegate (News.CYT369.Com.Domains.Document document)
               {
                   document.Read++;
               });
        }


        public News.CYT369.Com.Domains.Document GetDocumentByID(long id)
        {
            return base.GetOne<News.CYT369.Com.Domains.Document, News.CYT369.Com.Domains.DocumentExt>(c => c.DocExt, c => c.Id == id);
        }

        /// <summary>
        /// 获取所有的文章
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="total"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public IQueryable<News.CYT369.Com.Domains.Document> GetDocuments(int page, int rows, ref int total)
        {
            var items = base.Get<News.CYT369.Com.Domains.Document>();
            return items = this.Pagination(page, rows, ref total, items, c => c.Id);
        }

        /// <summary>
        /// 最热门的文章 top 10
        /// </summary>
        /// <returns></returns>
        public IQueryable<News.CYT369.Com.Domains.Document> GetHotDocuments()
        {
            var items = base.Get<News.CYT369.Com.Domains.Document, ICollection<News.CYT369.Com.Domains.DocAttachment>>(t => t.Attachments);
            return items.OrderByDescending(c => c.Read).Take(10);
        }

        #endregion

        #region Comments


        /// <summary>
        /// 保存评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public bool SaveDocumentComment(News.CYT369.Com.Domains.DocumentComment comment)
        {
            using (var ctx = base.Ctx)
            {
                var docUserId = (from o in ctx.Documents
                                 where o.Id == comment.DocID
                                 select o.EditorUserID).FirstOrDefault();

                if(docUserId > 0)
                {
                    comment.DocUserID = docUserId;
                }

                ctx.DocumentComments.Add(comment);
                ctx.SaveChanges();

                return true;
            }
        }

        public bool DeleteDocumentComment(long commentId)
        {
            using (var ctx = this.Ctx)
            {
                var item = (from o in ctx.DocumentComments
                            where o.Id == commentId
                            select o).FirstOrDefault();

                if (item != null)
                {
                    ctx.DocumentComments.Remove(item);
                    ctx.SaveChanges();

                    return true;
                }
            }

            return false;
        }
        #endregion

    }
}
